#!/bin/bash

# 系统管理后台启动脚本
# System Admin Platform Startup Script

set -e

echo "🚀 启动系统管理后台..."
echo "=================================="

# 检查Docker和Docker Compose是否安装
if ! command -v docker &> /dev/null; then
    echo "❌ Docker 未安装，请先安装 Docker"
    exit 1
fi

if ! command -v docker-compose &> /dev/null; then
    echo "❌ Docker Compose 未安装，请先安装 Docker Compose"
    exit 1
fi

# 创建必要的目录
echo "📁 创建必要的目录..."
mkdir -p backend/uploads
mkdir -p logs/{nginx,mysql,redis,backend,frontend}

# 设置环境变量文件
if [ ! -f backend/.env ]; then
    echo "📝 创建环境变量文件..."
    cp backend/.env.example backend/.env
    echo "⚠️  请编辑 backend/.env 文件配置数据库密码等信息"
fi

# 构建和启动服务
echo "🔨 构建和启动服务..."
docker-compose down --remove-orphans
docker-compose build --no-cache
docker-compose up -d

# 等待服务启动
echo "⏳ 等待服务启动..."
sleep 30

# 检查服务状态
echo "🔍 检查服务状态..."
docker-compose ps

# 显示访问信息
echo ""
echo "✅ 系统启动完成！"
echo "=================================="
echo "🌐 前端应用:        http://localhost"
echo "🔧 后端API:         http://localhost/api"
echo "📚 API文档:         http://localhost/api/docs"
echo "📊 Grafana监控:     http://localhost:3000 (admin/admin123)"
echo "📈 Prometheus:      http://localhost:9090"
echo "=================================="
echo "💡 默认管理员账号:"
echo "   用户名: admin"
echo "   密码:   admin123"
echo "=================================="

# 显示日志
echo "📋 实时日志 (Ctrl+C 退出):"
docker-compose logs -f